package face;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import util.*;
import objects.*;
import dispatchers.*;

public class Registration extends HttpServlet{
	DBUserDispatcher userDispatcher;
	
	public void init(){
		DBManager dbManager = new DBManager("anagram", "root", "root");
		userDispatcher = new DBUserDispatcher(dbManager, "users");
	}

	public void doPost(HttpServletRequest request, 
						HttpServletResponse response) throws ServletException, IOException{
		doGet(request, response);
	}

	public void doGet(HttpServletRequest request, 
						HttpServletResponse response) throws ServletException, IOException{
		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
	
		User user = new User();
		user.setLastName(request.getParameter("lastName"));
		user.setMiddleName(request.getParameter("middleName"));
		user.setName(request.getParameter("name"));
		user.setSecurityLevel(1);
		user.setMail(request.getParameter("mail"));
		user.setPassword(request.getParameter("password"));
		user.setRegistrationDate(new Date());
		
		userDispatcher.addUser(user);
		
		Article article = new Article(-1, "Ок!", "Вы зарегистрированы! И теперь можете войти на сайт!");
		
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/jsp/reportPage.jsp");
		dispatcher.forward(request, response);
	}
	
	private boolean isCorect(String[] args){
		for(int i=0; i<args.length; i++)
			if(args[i]==null || args[i].length()==0)
				return false;
		return true;
	}
	
}